package com.a2j.web.car.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.a2j.beans.base.BaseBean;
import com.a2j.beans.base.BasePageBean;
import com.a2j.beans.base.code.ResponseCode;
import com.a2j.beans.car.type.CarTypeBySeriesVO;
import com.a2j.beans.car.type.CarTypePO;
import com.a2j.beans.car.type.CarTypePageBO;
import com.a2j.beans.car.type.CarTypeVO;
import com.a2j.exception.BizException;
import com.a2j.utils.helper.PageHelper;
import com.a2j.web.car.mapper.CarTypeMapper;
import com.a2j.web.car.service.CarTypeService;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;

/**
 * Author: bzb
 * Data: 2022/1/19 15:37
 * Desc: 车型service实现
 */
@Service
public class CarTypeImpl implements CarTypeService {

    @Resource
    CarTypeMapper mapper;

    @Override
    public void addCarType(CarTypeVO record) {
        Assert.notNull(record, "车型数据为null!");
        Assert.notNull(record.getSeriesId(), "车系id为null!");
        Assert.notNull(record.getSpecificationId(), "车规id为null!");
        Assert.hasLength(record.getTypeName(), "车型名称为null!");

        CarTypePO typeInfo = CarTypePO.builder().build();
        BeanUtil.copyProperties(record, typeInfo);

        typeInfo.setId(null);
        typeInfo.setName(record.getTypeName());

        int result = mapper.addCarType(typeInfo);
        if (result == 0) throw new BizException(ResponseCode.FAILED_ADD);
    }

    @Override
    public void delCarType(Integer id) {
        Assert.notNull(id, "车型id为null!");

        int result = mapper.delCarType(id);
        if (result == 0) throw new BizException(ResponseCode.FAILED_DELETE);
    }

    @Override
    public void enable(Integer id, Integer enable) {
        Assert.notNull(id, "车型id为null!");

        CarTypePO po = CarTypePO.builder()
                .id(id)
                .enable(enable == null ? 0 : enable)
                .build();

        int result = mapper.updateCarTypeInfo(po);
        if (result == 0) throw new BizException(ResponseCode.FAILED_UPDATE);
    }

    @Override
    public void updateCarTypeInfo(CarTypeVO record) {
        Assert.notNull(record, "要更新的车型数据为null!");
        Assert.notNull(record.getTypeId(), "车型id为null!");

        CarTypePO typeInfo = CarTypePO.builder().build();
        BeanUtil.copyProperties(record, typeInfo);

        typeInfo.setId(record.getTypeId());
        typeInfo.setName(record.getTypeName());

        int result = mapper.updateCarTypeInfo(typeInfo);
        if (result == 0) throw new BizException(ResponseCode.FAILED_UPDATE);
    }

    @Override
    public BaseBean<CarTypeVO> getCarTypeDetail(Integer id) {
        Assert.notNull(id, "车型id为null!");

        CarTypeVO result = mapper.getCarTypeDetail(id);
        if (result == null) throw new BizException(ResponseCode.NO_DATA);

        return BaseBean.success(result);
    }

    @Override
    public BaseBean<BasePageBean<CarTypeVO>> getCarTypes(CarTypePageBO record) {
        Assert.notNull(record, "车型分页数据为null! 至少传个page");

        PageHelper.checkPage(record);

        List<CarTypeVO> result = mapper.getCarTypes(record);
        if (CollectionUtil.isEmpty(result)) throw new BizException(ResponseCode.NO_DATA);

        int total = mapper.selectCount(record);

        return new PageHelper<CarTypeVO>().transformData(
                record.getPage(),
                record.getRows(),
                total, result
        );
    }

    @Override
    public BaseBean<BasePageBean<CarTypeBySeriesVO>> getCarTypesBySeriesId(CarTypePageBO record) {
        Assert.notNull(record, "车型分页数据为null!");
        Assert.notNull(record.getSeriesId(), "车系id为null!");

        PageHelper.checkPage(record);

        List<CarTypeBySeriesVO> result = mapper.getCarTypesBySeriesId(record);
        if (CollectionUtil.isEmpty(result)) throw new BizException(ResponseCode.NO_DATA);

        int total = mapper.selectCount(record);

        return new PageHelper<CarTypeBySeriesVO>().transformData(
                record.getPage(),
                record.getRows(),
                total, result
        );
    }


    //--------------------------四个额外列表-------------------------------------------

    @Override
    public BaseBean<List<String>> getYearTypeList() {
        List<String> list = mapper.getYearTypeList();
        if (CollectionUtil.isEmpty(list)) throw new BizException(ResponseCode.NO_DATA);

        return BaseBean.success(list);
    }

    @Override
    public BaseBean<List<String>> getEffluentStandardList() {
        List<String> list = mapper.getEffluentStandardList();
        if (CollectionUtil.isEmpty(list)) throw new BizException(ResponseCode.NO_DATA);

        return BaseBean.success(list);
    }

    @Override
    public BaseBean<List<String>> getSaleStateList() {
        List<String> list = mapper.getSaleStateList();
        if (CollectionUtil.isEmpty(list)) throw new BizException(ResponseCode.NO_DATA);

        return BaseBean.success(list);
    }

    @Override
    public BaseBean<List<String>> getSizeTypeList() {
        List<String> list = mapper.getSizeTypeList();
        if (CollectionUtil.isEmpty(list)) throw new BizException(ResponseCode.NO_DATA);

        return BaseBean.success(list);
    }
}
